package com.mdinfo.gw.door.driver.utils;

/**
 * Created by Administrator on 2018/1/29.
 */
public class ByteUtil {



    public static String bytes2Hex(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * byte[]数组转16进制字符串
     */
    public static String bytes2Hex2(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
            stringBuilder.append(" ");
        }
        return stringBuilder.toString();
    }

    public static byte[] int2byte(Integer[] ints){
        byte[] bytes = new byte[ints.length];
        for(int i=0;i<ints.length;i++){
            bytes[i] = ints[i].byteValue();
        }
        return bytes;
    }

    public static Integer validate(Integer[] ints){
        int result = ints[0];
        for(int i=1;i<ints.length;i++){
            result = result ^ ints[i];
        }
        return result;
    }

    /**
     * 输入包头和数据
     * 添加包尾,并且转成byte[]
     */
    public static byte[] getBytes(Integer[] ints){
        if(ints==null || ints.length==0){
            return null;
        }
        Integer[] ints2 = new Integer[ints.length+2];
        for(int i=0;i<ints.length;i++){
            ints2[i] = ints[i];
        }
        Integer validate = validate(ints);
        ints2[ints.length] = validate;
        ints2[ints.length+1] = 0x03;


        return int2byte(ints2);
    }

    /**
     * 心跳回复
     */
    public static byte[] heartbeatAnswer(){
        Integer[] ints = {0x02,0xa0,0x56,0xff,0x00,0x02,0x00,0x5b,0xa0,0xf2,0x03};
        return ByteUtil.int2byte(ints);
    }

    /**
     * 刷卡响应
     */
    public static byte[] brushCardAnswer(Integer doorId,Integer brushId){
        Integer[] ints = {0x02,0xa0,0x53,0xff,doorId,0x01,0x00,brushId};
        return getBytes(ints);
    }

    /**
     * byte转hex
     */
    public static String byte2Hex(byte b){
        int v = b & 0xFF;
        String hv = Integer.toHexString(v);
        if(hv.length()==1){
            return "0"+hv;
        }else{
            return hv;
        }
    }

    /**
     * 获取门卡id字符串
     * byte转16进制字符串
     */
    public static String getCardId(byte ...bytes){

        String ss = "";
        for(byte b : bytes){
            String s = byte2Hex(b);
            ss = ss + s;
        }
        long l = Long.parseLong(ss, 16);
        return String.valueOf(l);
    }
}
